<?php
class IndexAction extends BaseAction{
    public function _empty() {
        $tpl = "../Include/App/Group/Bridge/Tpl/Public_404.html";
        $this->display($tpl);
    }
    
    public function index() {
        //$project =  M('project')->table(array('xia_project'=>'arr','xia_team'=>'team'))->field('arr.*,team.name as tname,team.logo')->where('arr.tid=team.id')->limit(6)->select();
        $tablerc = array('xia_mpindex'=>'mpi', 'xia_user_info'=>'uif', 'xia_account'=>'ac');
        $whererc = "mpi.stype=2 and mpi.ishow=1 and mpi.oid=uif.uid and uif.utype=1 and uif.uid=ac.uid and ac.emailverify=1 and ac.passcheck=1";//企业用户不显示
        $order  = "mpi.sortid";
        $rencai = M('user_info')->table($tablerc)->where($whererc)->order($order)->select();
//        $table = array('xia_user_info'=>'uif','xia_account'=>'ac');
//        $where = "(uif.uid IN (62,77,82,87,111,139)) AND uif.uid=ac.uid";
//        $order  = "mpi.sortid";
//        $uiTab = M('user_info');
//        $rencai = $uiTab->table($table)->field('*')->where($where)->order($order)->select();
        //隐私过滤
        $myinfo = $this->userinfo;
        for ($i=0;$i<count($rencai);$i++) {
            userInfoFilter($myinfo, $rencai[$i]);
            //若没填写昵称，则把电子邮件地址隐藏部分显示
            $rencai[$i]['nickname'] = convEmail($rencai[$i]['nickname']);
            //Log::write('rencai:'. implode(',', $rencai[$i]), Log::ERR);
        }
        
        $tablej = array('xia_mpindex'=>'mpi', 'xia_job'=>'job', 'xia_project'=>'prj', 'xia_team'=>'team', 'xia_user_info'=>'uif', 'xia_account'=>'ac');
        $fieldj = 'job.*,prj.id as pid,prj.name as pname,team.name as tname,uif.uid as cid,uif.nickname as creater, ac.emailverify,ac.passcheck,(SELECT COUNT(*) FROM xia_jobfav jf WHERE job.id=jf.jobid) as fc';
        $wherej = 'mpi.stype=1 and mpi.ishow=1 and mpi.oid=job.id and job.state>0 and job.ctime>0 and job.pid=prj.id and prj.state>0 and prj.tid=team.id and team.power=2 and team.uid=uif.uid and uif.uid=ac.uid and ac.passcheck<>0';
        $resJobs = M('job')->table($tablej)->field($fieldj)->where($wherej)->order($order)->select();
//        $sql = "SELECT job.*,prj.id as pid,prj.name as pname,team.name as tname,uif.uid as cid,uif.nickname as creater,ac.emailverify,ac.passcheck,(SELECT COUNT(*) FROM xia_jobfav jf WHERE job.id=jf.jobid) as fc FROM `xia_job` `job`,`xia_project` `prj`,`xia_team` `team`,`xia_user_info` `uif`,`xia_account` `ac` WHERE ((job.id IN (35,44,45,46,48,50)) and job.pid=prj.id and prj.state>0 and prj.tid=team.id and team.power=2 and team.uid=uif.uid and uif.uid=ac.uid and ac.passcheck<>0) ORDER BY job.ctime DESC LIMIT 6";
//        $resJobs = M('job')->query($sql);
        $resMfav = M('jobfav')->where('uid='.$this->uid)->select();
        $jobs = array();
        foreach ($resJobs as $job) {
            $job['faved'] = 0;
            if ($resMfav) {
                foreach ($resMfav as $fav) {
                    if ($fav['jobid']==$job['id']) {
                        $job['faved'] = 1;
                    }
                }
            }
            
            $job['creater'] = convEmail($job['creater']);
            $jobs[] = $job;
        }
        
        $this->assign("jobs", $jobs);
        $this->assign("talents", $rencai);
        $this->assign("on", 1);
        $this->display();
    }

    public function faq() {
        $this->display();
    }
    
    public function contactus() {
        $this->display();
    }
    
    public function aboutus() {
        $this->display();
    }   
    public function services() {
        $this->display();
    }
      public function conditions() {
        $this->display();
    }
      public function fees() {
        $this->display();
    }
        public function rules() {
        $this->display();
    }
     public function privacy() {
        $this->display();
    }
    
    //站外邀请
    public function join() {
        $token = I('token');
        $resMailinvite = M('mailinvite')->table(array('xia_mailinvite'=>'mi','xia_user_info'=>'user', 'xia_job'=>'job'))->field('mi.*, user.nickname as sendername, user.teamname, job.name as jname') ->where("mi.token='".$token."' and mi.sender=user.uid and mi.jid=job.id and job.state>0")->find();
        if (!$resMailinvite){
            $this->assign('timeout', 1);//视为超时
            $this->display();
            exit;
        }
        $elapse = time()-$resMailinvite['timepoint'];
        if ($elapse<=86400) {
            $this->assign('senderid', $resMailinvite['sender']);
            $this->assign('sender',$resMailinvite['sendername']);
            $this->assign('teamname', $resMailinvite['teamname']);
            $this->assign('jobname', $resMailinvite['jname']);
            $this->assign('token', $token);
        }
        else{//超出24小时，失效
            $this->assign('timeout', 1);
        }
        
        $this->display();
    }
    
    //邀请反馈
    public function replyInvite() {
        import('ORG.Util.Verify');
        if (Verify::isEmpty(I('reply'))==false
            || Verify::isEmpty(I('token'))==false) {
            exit;
        }
        $reply = I('reply');// 1为同意，2为拒绝
        $token = I('token');
        
        $resMailinvite = M('mailinvite')->table(array('xia_mailinvite'=>'mi','xia_team'=>'team','xia_project'=>'prj','xia_job'=>'job'))->field('mi.*, team.name as tname, team.uid as tleader, prj.name as pname, prj.uid as pleader, job.name as jname')->where("mi.token='".$token."' and mi.tid=team.id and mi.pid=prj.id and mi.jid=job.id and job.state>0")->find();
        if (!$resMailinvite) {
            exit;
        }
        
        if ($reply==2) {
            //通知邀请者
            $content = "<td>遗憾的说，您向邮箱地址是".$resMailinvite['receiver']."的朋友发出的<b>".$resMailinvite['pname']."</b>之<b>".$resMailinvite['jname']."</b>邀请被残忍拒绝了。</td>";
            $content = logPost($content, "", time(), "设为已读");
            sendNotice($resMailinvite['sender'], $content);
            
            M('mailinvite')->where("token='".$token."'")->delete();
            echo "<script>window.location.href='".U('/@'.getDomain())."';</script>";
            exit;
        }
        elseif ($reply==1){
            //自动注册
            $now = time();
            $pwd = randCode(6);
            $resUser = M('user')->add(array(
                'email'=>$resMailinvite['receiver'],
                'pwd'=>md5($pwd),
                'ctime'=>$now,
            ));
            if (!$resUser) {
                echo '<script type="text/javascript">require(["jquery","modules/message"],function($,messagebox){$(function(){messagebox.alert({content:"该email已被注册!", sure:function(){window.location.href="'.U('/@'.getDomain()).'"}});});});</script>"';
                exit;
            }
            
            //删除链接
            M('mailinvite')->where("token='".$token."'")->delete();
            
            $infodata = array(
                'uid'=>$resUser,
                'utype'=>1,
                'nickname'=>$resMailinvite['receiver'],
                'uptime'=>$now,
            );
            //自动设置为已激活
            $acData = array(
                'uid' => $resUser,
                'emailverify' => 1,
            );
            M('account')->add($acData);
            
            $jobTab = M('job');
            //该职位是否已托管
            $resJob = $jobTab->where('id='.$resMailinvite['jid'])->find();
            if (!$resJob) {
                ajaxf('找不到该职位！');
            }
//            if ($resJob['moneyin']==0) {//暂时取消托管限制 2015-11-20
//                ajaxf('这个职位薪酬尚未托管,不能入职');
//            }
            //是否满员
            if ($resJob['num']<=$resJob['datastate']) {//职位已满员
                $resUserinfo = M('user_info')->add($infodata);
                echo "<script type='text/javascript'>require(['jquery','modules/message'],function($,messagebox){messagebox.alert({content:'你已是般若虾会员，请记住你的账号密码：<br/>ID:".$resMailinvite['receiver']."<br/>密码：".$pwd."<br/>只是很遗憾，该职位已满员，无法加入。你可以看看其他职位。', sure:function(){window.location.href='".U('/@'.getDomain())."'}});});</script>";
                exit;
            }
            //职位未满员
            $infodata['projectname'] = $resMailinvite['pname'];
            $infodata['projectid'] = $resMailinvite['pid'];
            $infodata['teamname'] = $resMailinvite['tname'];
            $infodata['teamid'] = $resMailinvite['tid'];
            $resUserinfo = M('user_info')->add($infodata);
            
            $resJob = $jobTab->where('id='.$resMailinvite['jid'])->setInc('datastate');//职位在职数增加
            M('team')->where('id='.$resMailinvite['tid'])->setInc('jobsok');//团队职位总席位在职数增加
            if (!$resJob) {
                echo "<script type='text/javascript'>require(['jquery','modules/message'],function($,messagebox){messagebox.alert({content:'职位数据错误!', sure:function(){window.location.href='".U('/@'.getDomain())."'}});});</script>";
                exit;
            }
            $resJobuser = M('job_user')->add(array(
                'uid'=>$resUser,
                'pid'=>$resMailinvite['pid'],
                'did'=>$resMailinvite['did'],
                'jobid'=>$resMailinvite['jid'],
                'state'=>2,
                'uname'=>$resMailinvite['receiver'],
                'uptime'=>$now,
            ));
            if (!$resJobuser) {
                echo "<script type='text/javascript'>require(['jquery','modules/message'],function($,messagebox){messagebox.alert({content:'任职数据错误!', sure:function(){window.location.href='".U('/@'.getDomain())."'}});});</script>";
                exit;
            }
            
            //记入任职历史
            $hisData = array(
                'uid' => $resUser,
                'jobid' => $resMailinvite['jid'],
                'prjid' => $resMailinvite['pid'],
                'teamid' => $resMailinvite['tid'],
                'jointime' => $now,
                'quitime' => '0',
                'quitype' => \App\Common\Constvars::JOBHIS_QUITYPE_UNQUIT,
            );
            M('jobhistory')->add($hisData);
            //发送通知
            $content = "喜大普奔！您向<a href='".genTalentURL($resUser)."'><b>[".$resMailinvite['receiver']."]</b></a>发出的<b>".$resMailinvite['pname']."</b>之\"".$resMailinvite['jname']."\"之邀请，已成功吸引TA加入您的团队，快去<a href='".U('Team/members/@'.getDomain(),array('teamid'=>$resMailinvite['tid']))."'><b>[成员管理]</b></a>看看吧";
            $notice = array();
            $notice[] = array(
                'uid'=>$resMailinvite['tleader'],
                'content'=>$content,
                'ctime'=>$now,
            );
            if ($resMailinvite['tleader']!=$resMailinvite['pleader']) {
                $notice[] = array(
                    'uid'=>$resMailinvite['pleader'],
                    'content'=>$content,
                    'ctime'=>$now,
                );
            }
            //给被邀请者发送情报
            $notice[] = array(
                'uid'=>$resUser,
                'content'=>'您经邮件邀请，已加入团队<a href="'.U('Team/main/@'.getDomain()).'"><b>'.$resMailinvite['tname'].'</b></a>，祝您工作愉快！',
                'ctime'=>$now,
            );
            M('office_log')->addAll($notice);
            
            echo "<script>require(['jquery','modules/message'],function($,messagebox){messagebox.alert({content:'恭喜你！你已成为般若虾用户,并已是团队".$resMailinvite['tname']."的成员。<br/>你的账户/密码如下，请不要忘记。<br/>ID:".$resMailinvite['receiver']."<br/>密码：".$pwd."', sure:function(){window.location.href='".U('/@'.getDomain())."';}});});</script>";
            exit;
        }
    }
    
    //接收用户反馈
    public function feedback_ajax() {
        $msg = I('msg', '');
        if ($msg=='') {
            echo json_encode(array('success'=>0, 'msg'=>'参数错误'));
            exit;
        }
        
        $sender = I('sender', '');
        $phone = I('phone', '');
        $email = I('email', '');
        
        $content = "sender:".$sender.",phone:".$phone.",email:".$email.",msg:".$msg;
        $feedback = array(
            'content' => $content,
            'uptime' => time(),
        );
        $ret = M('user_feedback')->add($feedback);
        if ($ret) {
            ajaxs(1, '发送成功');
        }
        ajaxf('未发送成功!');
    }
    public function questions() {$this->display();}
    public function userrules() {$this->display();}
    public function sp(){
        $this->display();
    }
}
?>